<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Hash;
use App\Models\Services;
use App\Models\Users;
use App\Models\Helpers;
use App\Models\Linkslists;
use App\Models\Linkslistsminapps;
use App\Models\Linkslistsimages;
use App\Models\Memberorders;
use App\Models\Usersinvitations;
use App\Models\Usersremarks;
use App\Models\Usersinfo;
use App\Models\Userslogs;
use App\Models\Userswithdrawals;
use App\Models\Usersincomes;
use App\Models\Usersinvoices;
use App\Models\Usersauthority;
use App\Models\Usersqrcodelists;
use App\Models\Usersdomain;
use App\Models\Worklink;
use App\Services\UploadService;
use App\Services\WechatService;
use App\Services\AlipayService;
use App\Services\CommonService;
use App\Services\OutputService;
use App\Services\CacheService;
use App\Jobs\Htmlosshanndle;
use App\Jobs\Wxkfhanndle;
use EasyWeChat\Factory;
use Auth,Pay,Exception,Log,Config;
use OSS\OssClient;
use OSS\Core\OssException;
use GuzzleHttp\Client;

class UsersController extends Controller
{
    public function usersAdd(Request $request)
    {
        return view('admin.users.users_add');
    }

    public function usersPhone(Request $request)
    {
        return view('admin.users.users_phone');
    }

    public function usersPhoneSubmit(Request $request)
    {
        if (empty($request->username) && mb_strlen($request->username) != 11) return back()->with("status","error")->with("msg","请输入正确的手机号码！")->withInput();
        if (empty($request->password) && mb_strlen($request->password) < 10) return back()->with("status","error")->with("msg","请输入10个字以上用户密码！")->withInput();
        $password = Hash::make($request->password);
        $users = Auth::user();
        $existUsers = Users::getUserByUserName($request->username);
        if (!empty($existUsers)) return back()->with("status","error")->with("msg","账号已存在,请直接登录即可!")->withInput();
        Users::phoneBind($users->id,$request->username,$password);
        return redirect("/app");
    }

    public function usersAddSubmit(Request $request)
    {
        if (empty($request->nike) && mb_strlen($request->nike) < 3) return back()->with("status","error")->with("msg","请输入3个字以上用户昵称！")->withInput();
        if (empty($request->username) && mb_strlen($request->username) < 11) return back()->with("status","error")->with("msg","请输入正确的手机号码！")->withInput();
        if (empty($request->password) && mb_strlen($request->password) < 10) return back()->with("status","error")->with("msg","请输入10个字以上用户密码！")->withInput();
        $user = Auth::user();
        if ($user->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权操作本功能！");
        $users = Users::getAdminByUsername($request->username);
        if (!empty($users)) return back()->with("status","error")->with("msg","账户已存在,无法重新添加！");
        $password = Hash::make($request->password);
        $authority = $user->authority == 3 ? $request->authority : 1;
        $users_id = Users::adminAdd($request->username,$password,$request->nike,$authority);
        Usersauthority::createUsersAuthority($users_id,1,1);
        if ($user->authority == 2) {
            Usersinvitations::addUsersInvitation($users_id,$user->id);
            return redirect("/admin/links_lists/users_agent_lists")->with("status","success")->with("msg","用户添加成功！");
        } else {
            return redirect("/admin/links_lists/users_lists")->with("status","success")->with("msg","用户添加成功！");
        }
    }

    public function alipayBack(Request $request)
    {
        try {
            $data = Pay::alipay(config('pay.alipay'))->verify();
            $order = Memberorders::getByOrderNo($data->out_trade_no);
            if (empty($order)) return redirect("/admin/links_lists/member_orders")->with("status","error")->with("msg","订单不存在！");

            // 修改订单状态
            Memberorders::updatePayStatus($data->out_trade_no, [
                'pay_status'    => 3,
                'transaction_id'=> $data->trade_no,
                'type'          => 2,
            ]);
            if ($order->product_id == 2) {
                $limit_pv = 50000;
                $limit_status = 1;
            } elseif ($order->product_id == 3) {
                $limit_pv = 500000;
                $limit_status = 1;
            } elseif ($order->product_id == 4) {
                $limit_pv = 1000000;
                $limit_status = 2;
            } else {
                $limit_pv = 100;
                $limit_status = 1;
            }
            // 把全部订单改为可用
            Linkslists::updateLinksListsStatus($order->users_id,$limit_status,$limit_pv,$order->end_time);
            Worklink::updateWorkLinkMember($order->users_id,$limit_pv,$order->end_time);
            Userslogs::addUsersLogs($order->users_id, "会员充值成功！","您的会员充值成功,充值方式: 微信 会员到期时间:".date("Y-m-d",$order->end_time),$order->id);

            $itations = Usersinvitations::getUsersInvitation($order->users_id);
            // 判断是否有邀请人
            if (!empty($itations)) {
                $batch = date("Ym",time());
                $incomes = Usersincomes::getbatchIncomes($itations->users_invitation_id,$batch);
                if (empty($incomes)) {
                    $commission = $order->price*0.1;
                    Usersincomes::addbatchIncomes($itations->users_invitation_id,$batch,$order->price,$commission);
                } else {
                    if ($incomes->money <= 1000) {
                        $commission = $order->price*0.1;
                    } elseif ($incomes->money <= 5000) {
                        $commission = $order->price*0.2;
                    } elseif ($incomes->money <= 10000) {
                        $commission = $order->price*0.3;
                    }
                    $money = $order->price+$incomes->money;
                    $commission = $commission+$incomes->commission;
                    Usersincomes::updatebatchIncomes($incomes->id,$money,$commission);
                }
            }
            Users::updateUsersMemberTime($order->users_id,$order->product_id,$order->end_time);
            // 更新线上OSS
            Htmlosshanndle::dispatch($order->users_id,"","")->delay(10);
            Wxkfhanndle::dispatch($order->users_id)->delay(10);
            return redirect("/admin/links_lists/member_orders")->with("status","success")->with("msg","订单支付成功！");
        } catch (Exception $e) {
            return redirect("/admin/links_lists/member_orders")->with("status","success")->with("msg","订单支付失败,请重新支付！");
        }
    }

    public function usersOrdersLists(Request $request)
    {
        $users_id = Auth::user()->id;
        $orders = Memberorders::usersMemberOrders($users_id);
        return view('admin.users.members_order_lists',['orders'=>$orders]);
    }

    public function memberOrdersInvoice(Request $request)
    {
        $users = Auth::user();
        if ($users->authority == 3 && !empty($request->users_id)) {
            $users_id = $request->users_id;
        } else {
            $users_id = $users->id;
        }
        $orders = Memberorders::usersUnInvoiceMemberOrders($users_id);
        $invoices = Usersinvoices::getUsersLastInvoices($users_id);
        return view("admin.users.members_order_invoice",["orders"=>$orders,"invoices"=>$invoices,"users_id"=>$users_id]);
    }

    public function memberOrdersInvoiceSubmit(Request $request)
    {
        if (mb_strlen($request->company_name,'utf8') <= 5) return back()->with("status","error")->with("msg","请输入正确的公司名称！");
        // if (mb_strlen($request->company_bank_name,'utf8') <= 5) return back()->with("status","error")->with("msg","请输入正确的银行名称！");
        // if (mb_strlen($request->company_bank_card,'utf8') <= 5) return back()->with("status","error")->with("msg","请输入正确的银行卡号！");
        if (mb_strlen($request->company_tax_number,'utf8') <= 5) return back()->with("status","error")->with("msg","请输入正确的公司税号！");
        // if (mb_strlen($request->company_address,'utf8') <= 5) return back()->with("status","error")->with("msg","请输入正确的公司地址！");
        if (mb_strlen($request->email,'utf8') <= 5) return back()->with("status","error")->with("msg","请输入正确的邮箱地址！");
        $users = Auth::user();
        if ($users->authority == 3 && !empty($request->users_id)) {
            $users_id = $request->users_id;
        } else {
            $users_id = $users->id;
        }
        $orders = Memberorders::usersUnInvoiceMemberOrders($users_id);
        if ($orders <= 50) return back()->with("status","error")->with("msg","申请发票金额不得低于50元！");
        // 更新订单开票状态
        Memberorders::updateOrdersInvoicesStatus($users_id);
        // 保存开票信息
        $company_tax_number = str_replace(" ","",$request->company_tax_number);
        Usersinvoices::insertInvoices($users_id,$request->company_name,$company_tax_number,$request->email,$orders);
        return back()->with("status","success")->with("msg","申请成功，发票将会在2个工作日内发送到您的 ".$request->email." 邮箱。");
    }

    public function usersInvoice(Request $request)
    {
        $users = Auth::user();
        $invoices = Usersinvoices::getUsersInvoices($users->id,$users->authority,$request->title);
        return view('admin.users.invoices',['invoices'=>$invoices,'condition'=>$request->all()]);
    }

    public function usersInvoiceDetial(Request $request)
    {
        $users = Auth::user();
        if ($users->authority != 3) return redirect("/admin/users/users_invoice")->with("status","error")->with("msg","您无权操作本功能！");
        if (empty($request->images)) return back()->with("status","error")->with("msg","请上传正确的照片！");
        $images = UploadService::uploadAlOss($request->images);
        Usersinvoices::updateStatus($request->id,$images);
        return back()->with("status","success")->with("msg","更新成功！");
    }

    public function memberOrdersRePay(Request $request,$id)
    {
        $orders = Memberorders::ordersDetialById($id);
        if (empty($orders)) return back()->with("status","error")->with("msg","订单不存在,无法支付！");
        if ($orders->pay_status != 2) return back()->with("status","error")->with("msg","订单非待支付状态,无法支付！");
        $users_id = Auth::user()->id;
        if ($orders->users_id != $users_id) return back()->with("status","error")->with("msg","本订单非您的订单,无法支付！");
        $user  = Users::getUsersById($users_id);
        $out_trade_no = md5(time().rand("10000","99999"));
        Memberorders::updateOrdersmemberInfo($id,["order_no"=>$out_trade_no]);

        $order = [
            'out_trade_no' => $out_trade_no,
            'total_amount' => $orders->price,
            'subject' => $orders->name,
        ];
        $alipay = Pay::alipay(config('pay.alipay'))->web($order);

        return $alipay;
    }

    public function agentCode(Request $request)
    {
        $users_id = Auth::user()->id;
        $type = empty($request->type) ? 1 : $request->type;
        $invitation_code = CommonService::aesEncrypt($users_id);
        // if (empty(Auth::user()->invitation_qrcode)) {
        //     $minapps = Linkslistsminapps::getLinksListsMinappsById(2);
        //     $contents = WechatService::wxAcodeUnlimit($minapps->appid,$minapps->secret,"pages/index/index",$users_id);
        //     $imagepath = "images/".time().$users_id.".jpg";
        //     Storage::put($imagepath, $contents);
        //     $path = Storage::path($imagepath);
        //     Image::make('images/invitation_qrcode.jpg')->resize(900, 1600)->insert($path, 'bottom-center', 0, 100)->save($imagepath);
        //     Storage::delete($imagepath);
        //     $invitation_qrcode = UploadService::uploadLoacltoOss($imagepath);
        //     Users::updateInvitationQrcode($users_id,$invitation_qrcode);
        // } else {
        //     $invitation_qrcode = Auth::user()->invitation_qrcode;
        // }

        return view('admin.users.agent_code',['invitation_code'=>urlencode($invitation_code),'type'=>$type]);
    }

    public function withdrawal(Request $request)
    {
        $alipay = AlipayService::cleanAlipayNumber($request->alipay);
        $money = Auth::user()->money;
        $users_id = Auth::user()->id;

        if ($request->money <= 0) return back()->with("status","error")->with("msg","您没有可提现金额!")->withInput();
        if ($money < $request->money) return back()->with("status","error")->with("msg","超过可提现金额!")->withInput();
        $order_id = md5(date("YmdH").$users_id);
        $id = Userswithdrawals::addUsersWithdrawals($users_id,$alipay,$request->name,$request->money);

        $transfer = AlipayService::alipayTransfer($order_id,$request->money,$alipay,$request->name);
        if ($transfer->code == 10000) {
            Users::decrementUsersMoney($users_id, $request->money);
            Userswithdrawals::updateUsersWithdrawalsStatus($id,2);
            return redirect("/admin/users/agent_code")->with("status","success")->with("msg","提现成功,已提现".$request->money."元！");
        } else {
            return back()->with("status","error")->with("msg",$transfer->sub_msg)->withInput();
        }
    }

    public function usersWithdrawalLists(Request $request)
    {
        $status = empty($request->status) ? 2 : $request->status;
        $userswithdrawals = Userswithdrawals::getWithdrawalLists($status);
        return view('admin.users.users_withdraw_lists',['userswithdrawals'=>$userswithdrawals,'condition'=>$request->all()]);
    }

    public function wechatBind(Request $request)
    {
        $users = Auth::user();
        if (!empty($users->wechat_openid)) return back()->with("status","error")->with("msg","您已经绑定过微信！");
        $config = Config::get("wechat_login.setting");
        $app = Factory::officialAccount($config);
        $url = $app->oauth->scopes(['snsapi_login'])->redirect($request->url()."_back");
        return redirect()->away($url);
    }

    public function wechatBindBack(Request $request)
    {
        $config = Config::get("wechat_login.setting");
        try {
            $app = Factory::officialAccount($config);
            $user = $app->oauth->userFromCode($request->code);
            $getOriginal = $user->getRaw();
        } catch (Exception $e) {
            $message = $e->getMessage();
            return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","微信绑定失败！");
        }

        if (empty($user->getId())) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","微信绑定失败！");

        $users = Users::getUsersByUnionid($getOriginal["unionid"]);
        $address = $getOriginal["country"].$getOriginal["province"].$getOriginal["city"];

        if (empty($users)) {
            Users::bindWebOpenid(Auth::user()->id,$getOriginal["openid"],$getOriginal["headimgurl"],$getOriginal["nickname"],$getOriginal["sex"],$address,$getOriginal["unionid"]);
        } else {
            return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您的微信已绑定或其他账号，无法重复绑定！");
        }
        return redirect("/admin/links_lists/lists")->with("status","success")->with("msg","微信绑定成功！");
    }

    public function resetPassword(Request $request)
    {
        return view('admin.users.reset_password');
    }

    public function resetPasswordSubmit(Request $request)
    {
        if (empty($request->password) || empty($request->repassword)) return back()->with("status","error")->with("msg","请输入新密码和确认密码！");
        if ($request->password != $request->repassword) return back()->with("status","error")->with("msg","两次输入的密码不一致！");
        if (mb_strlen($request->password) > 15) return back()->with("status","error")->with("msg","密码长度不能超过15个字");
        $password = Hash::make($request->password);
        Users::updateUsersPassword(Auth::user()->id,$password);
        return back()->with("status","success")->with("msg","密码修改成功,新密码为: ".$request->password);
    }

    public function usersDetial(Request $request,$users_id="")
    {
        $authority = Auth::user()->authority;
        if ($authority != 3 && !empty($users_id)) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权操作本功能！");
        $users_id = empty($users_id) ? Auth::user()->id : $users_id;
        $users = Users::getUsersDetial($users_id);
        if (empty($users)) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","用户不存在！");
        $links = Linkslists::adminGetUsersLinks($users_id);
        $links_lists_minapps = Linkslistsminapps::getUsersAllMinapps($users_id);
        if ($authority != 3) {
            $memberorders = Memberorders::usersDetialMemberOrders($users_id);
            $lastorder = $memberorders->where("pay_status",3)->first();
        } else {
            $memberorders = Memberorders::usersMemberOrders($users_id);
            $lastorder = $memberorders->where("pay_status",3)->first();
        }
        $userInfo = Usersinfo::getUsersInfoDetial($users_id);
        return view('admin.users.users_detial',['users'=>$users,'links'=>$links,'links_lists_minapps'=>$links_lists_minapps,'memberorders'=>$memberorders,'lastorder'=>$lastorder,'userInfo'=>$userInfo]);
    }

    public function apiUsersDetial(Request $request,$users_id="")
    {
        $authority = Auth::user()->authority;
        if ($authority != 3 && !empty($users_id)) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权操作本功能！");
        $users_id = empty($users_id) ? Auth::user()->id : $users_id;
        $nav = empty($request->nav) ? 1 : $request->nav;
        $users = Users::getUsersDetial($users_id);
        if(empty($users)) return OutputService::error("用户不存在!");
        $limit = empty($request->limit) ? 20 : $request->limit;
        if($nav == 1){
            $links = Linkslists::adminGetUsersLinks($users_id,$request->title,$limit);
            return OutputService::success('SUCCESS',["data"=>$links]);
        }elseif($nav == 2){
            $links_lists_minapps = Linkslistsminapps::getUsersAllMinapps($users_id,$request->name,$limit);
            return OutputService::success('SUCCESS',["data"=>$links_lists_minapps]);
        }elseif($nav == 3){
            $memberorders = Memberorders::usersCenterMemberOrders($users_id,$limit);
            return OutputService::success('SUCCESS',["data"=>$memberorders]);
        }elseif($nav == 4){
            $users_withdrawals = Userswithdrawals::getUsersWithdrawalLists($users_id,$limit);
            return OutputService::success('SUCCESS',["data"=>$users_withdrawals]);
        }elseif($nav == 5){
            if($authority == 3){
                $remarks = Usersremarks::usersRemarksLists($users_id,$limit);
                return OutputService::success('SUCCESS',["data"=>$remarks]);
            }
        }elseif($nav == 7){
            $users = Usersinvitations::getAgentUsersLists($users_id,$request->nike,$limit);
            $users_ids = $users->pluck("id")->all();
            $memberorders = Memberorders::getUsersLastOrders($users_ids);
            $products = Memberorders::$products;
            foreach ($users as $user) {
                $memberorder = $memberorders->where("users_id",$user->id)->first();
                if (!empty($memberorder)) {
                    $product = collect($products)->where("id",$memberorder->product_id)->first();
                    $user->product_id = $product["title"];
                    if ($memberorder->end_time < time()) {
                        $user->member_time = "已过期";
                    } else {
                        $user->member_time = date("Y-m-d",$memberorder->end_time);
                    }
                } else {
                    $user->member_time = "-";
                    $user->product_id = "-";
                }
            }
            return OutputService::success('SUCCESS',["data"=>$users]);
        }
        return OutputService::error("参数错误");
    }

    public function remarksAdd(Request $request)
    {
        $authority = Auth::user()->authority;
        if ($authority != 3) return response()->json(["status"=>"error","code"=>"2001","msg"=>"您无权操作本功能！"]);
        if (empty($request->users_id)) return response()->json(["status"=>"error","code"=>"2001","msg"=>"用户ID不能为空！"]);
        if (empty($request->title)) return response()->json(["status"=>"error","code"=>"2001","msg"=>"备注标题不能为空！"]);
        if (empty($request->describe)) return response()->json(["status"=>"error","code"=>"2001","msg"=>"备注详情不能为空！"]);
        if (mb_strlen($request->title) >= 50) return response()->json(["status"=>"error","code"=>"2001","msg"=>"备注标题过长,请不要超过50个字符！"]);
        if (mb_strlen($request->describe) >= 255) return response()->json(["status"=>"error","code"=>"2001","msg"=>"备注描述过长,请不要超过255个字符！"]);
        Usersremarks::addUsersRemarks($request->users_id,$request->title,$request->describe);
        return response()->json(["status"=>"success","code"=>"1000","msg"=>"备注添加成功！"]);
    }

    public function linksListsImages(Request $request)
    {
        $authority = Auth::user()->authority;
        if ($authority != 3) return response()->json(["status"=>"error","code"=>"2001","msg"=>"您无权操作本功能！"]);
        $images = Linkslistsimages::getLinksListsImages($request->users_id);
        return view('admin.links_lists.links_lists_images',['images'=>$images,'condition'=>$request->all()]);
    }

    public function usersAuthority(Request $request,$users_id)
    {
        $authority = Auth::user()->authority;
        if ($authority != 3) return response()->json(["status"=>"error","code"=>"2001","msg"=>"您无权操作本功能！"]);
        $users = Users::getUsersDetial($users_id);
        $usersauthority = Usersauthority::getUsersAuthority($users_id);
        if (empty($usersauthority)) {
            Usersauthority::createUsersAuthority($users_id,1,1);
            $usersauthority = Usersauthority::getUsersAuthority($users_id);
        }
        return view('admin.users.users_authority',['usersauthority'=>$usersauthority,'users'=>$users]);
    }

    public function usersAuthoritySubmit(Request $request,$users_id)
    {
        $authority = Auth::user()->authority;
        if ($authority != 3) return response()->json(["status"=>"error","code"=>"2001","msg"=>"您无权操作本功能！"]);
        // 更新用户权限
        Usersauthority::updateUsersAuthority($users_id,$request->miniapp_style,$request->miniapp_third,$request->miniapp_publish,$request->api_token,$request->api_urlscheme,$request->third_api,$request->lists_htmls);
        if($request->miniapp_style == 2){
            Linkslists::updateUsersMiniappstatus($users_id,1);
        }elseif ($request->miniapp_style == 3) {
            Linkslists::updateUsersMiniappstatus($users_id,3);
        }else{
            Linkslists::updateUsersMiniappstatus($users_id,2);
        }
        CacheService::usersMiniappCacheRemove($users_id);
        return redirect("/admin/users/users_detial/".$users_id)->with("status","success")->with("msg","用户权限修改成功！");
    }

    public function usersQrcodeLists(Request $request)
    {
        $user = Auth::user();
        if ($user->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权操作本功能！");
        $qrcodeLists = Usersqrcodelists::getUsersQrcodeLists($request->users_id,$request->links_id,$request->status,$request->created_start,$request->created_end);
        return view("admin.users.users_qrcode_lists",["qrcodeLists"=>$qrcodeLists,"condition"=>$request->all()]);
    }

    public function usersUpdate(Request $request,$users_id="")
    {
        if (Auth::user()->authority != 3 && !empty($users_id)) return redirect("/admin/users/users_detial")->with("status","error")->with("msg","您无权操作本功能！");
        $users_id = empty($users_id) ? Auth::user()->id : $users_id;
        $users = Users::getUsersDetial($users_id);
        $userInfo = Usersinfo::getUsersInfoDetial($users_id);
        if (empty($userInfo)) {
            Usersinfo::createUsersInfo($users_id,"","","","");
            $userInfo = Usersinfo::getUsersInfoDetial($users_id);
        }
        return view('admin.users.users_update',["users"=>$users,"userInfo"=>$userInfo]);
    }

    public function usersUpdateSubmit(Request $request,$users_id="")
    {
        if (Auth::user()->authority != 3 && !empty($users_id)) return redirect("/admin/users/users_detial")->with("status","error")->with("msg","您无权操作本功能！");
        $users = Auth::user();
        $users_id = empty($users_id) ? Auth::user()->id : $users_id;

        $userDetial = Users::getUsersDetial($users_id);
        if ($users->authority == 3) {
            if ($request->username != $userDetial->username) {
                Users::updateUsersName($users_id,$request->username);
            }
            if ($userDetial->disable != $request->disable) {
                Users::updateDsiable($users_id,$request->disable);
                if($request->disable == 2){
                    CacheService::usersMiniappCacheRemove($users_id);
                    Linkslists::updateUsersLinksListsDisable($users_id,2);
                }
            }
        }
        $authority = ($users->authority == 3) ? $request->authority : $userDetial->authority;
        $money = ($users->authority == 3) ? $request->money : $userDetial->money;

        Users::updateUsersInfo($users_id,$request->nike,$authority,$money);
        Usersinfo::updateUsersInfo($users_id,$request->email,$request->wechat,$request->remark,$request->comefrom);
        return back()->with("status","success")->with("msg","用户信息更新成功！");
    }

    public function domainList(Request $request)
    {
        $users = Auth::user();
        $usersDomains = Usersdomain::getUsersDomainList($users->id);
        if ($request->type == "json") {
            return OutputService::success("SUCCESS",["usersDomains"=>$usersDomains]);
        } else {
            return view("admin.users.users_domain",["usersDomains"=>$usersDomains]);
        }
    }

    public function domainAdd(Request $request)
    {
        return view("admin.users.users_domain_add");
    }

    public function domainAddSubmit(Request $request)
    {
        if (empty($request->prefix)) return OutputService::error("请输入主机记录!");
        if (empty($request->domain)) return OutputService::error("请输入解析域名!");
        try {
            $client = new Client;
            $response = $client->request("GET","http://".$request->prefix.".".$request->domain."/domain/check.txt");
            $body = $response->getBody()->getContents();
            if ($body != 1) return OutputService::error("未检测到域名解析,请解析绑定后2分钟后点击确认!");
        } catch (Exception $e) {
            return OutputService::error("未检测到域名解析,请解析绑定后2分钟后点击确认!");
        }
        $users = Auth::user();
        Usersdomain::createUsersDomain($users->id,$request->prefix.".".$request->domain);
        return OutputService::success("域名绑定成功!");
    }

    public function domainDelete(Request $request,$id)
    {
        $users = Auth::user();
        Usersdomain::userDomainDelete($users->id,$id);
        return redirect("/admin/users/domain_list")->with("status","success")->with("msg","绑定域名删除成功!");
    }

    public function updateStatus(Request $request)
    {
        $user = Auth::user();
        if ($user->authority != 3) return OutputService::error("暂无权限!");
        if(empty($request->users_id) || empty($request->status)) return OutputService::error("参数错误，请联系客服!");
        Users::updateStatus($request->users_id,$request->status);
        return OutputService::success("跟进状态更新成功!");
    }

}
